package com.hngy.xpq.android_practice.http.map;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.hngy.xpq.android_practice.MerchantActivity;
import com.hngy.xpq.android_practice.MerchantRegist;
import com.hngy.xpq.android_practice.R;

import java.util.ArrayList;

public class MerchantAddMap extends AppCompatActivity {

    private TextView textView;
    private boolean isPermissionRequested = false;

    private MapView mMapView = null;
    private BaiduMap baiduMap = null;
    private Button qiehuan,kaiqi;
    private LocationClient locationClient;
    SharedPreferences map;
    String la = null,lo = null;

    private boolean isFirstLoc = true;

    public View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.qiehuan) {
                if (baiduMap.getMapType() == BaiduMap.MAP_TYPE_NORMAL) {
                    baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                } else {
                    baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                }
            } else if (v.getId() == R.id.kaiqi) {
                if (!baiduMap.isTrafficEnabled()) {
                    baiduMap.setTrafficEnabled(true);
                } else {
                    baiduMap.setTrafficEnabled(false);
                }
            }
        }
    };

    public void addOpt() {
        LatLng point = new LatLng(39.915119, 116.403963);
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.tab);
        OverlayOptions options = new MarkerOptions().position(point).icon(bitmap);
        baiduMap.addOverlay(options);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_add_map);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        baiduMap = mMapView.getMap();
        qiehuan = (Button) findViewById(R.id.qiehuan);
        kaiqi = (Button) findViewById(R.id.kaiqi);
        qiehuan.setOnClickListener(clickListener);
        requestPermission();

        //本地存储
        map = super.getSharedPreferences("map",MODE_PRIVATE);

        kaiqi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = map.edit();
                editor.putString("bdLocation" , la);
                editor.putString("longitude" , lo);
                editor.commit();
                Intent intent = new Intent(MerchantAddMap.this, MerchantRegist.class);
                startActivity(intent);
            }
        });

        locationClient = new LocationClient(this);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setCoorType("bd09ll");
        option.setScanSpan(5000);
        locationClient.setLocOption(option);
        locationClient.registerLocationListener(new MyLocationListener());
        locationClient.start();
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null || baiduMap == null) {
                return;
            }
            baiduMap.setMyLocationEnabled(true);
            MyLocationData data = new MyLocationData.Builder()
                    .latitude(bdLocation.getLatitude())
                    .longitude(bdLocation.getLongitude())
                    .direction(bdLocation.getDirection())
                    .accuracy(bdLocation.getRadius())
                    .build();
            baiduMap.setMyLocationData(data);

            if (isFirstLoc) {
                isFirstLoc = false;
                float f = baiduMap.getMaxZoomLevel();
                LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, f-4);
                baiduMap.animateMapStatus(u);
            }

            la = String.valueOf(bdLocation.getLatitude());
            lo = String.valueOf(bdLocation.getLongitude());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        locationClient.stop();
        baiduMap.setMyLocationEnabled(false);
        super.onDestroy();
        mMapView.onDestroy();
    }



    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23 && !isPermissionRequested) {
            isPermissionRequested = true;
            ArrayList<String> permissionsList = new ArrayList<>();
            String[] permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
            };

            for (String perm : permissions) {
                if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(MerchantAddMap.this, perm)) {
                    permissionsList.add(perm);
                    // 进入到这里代表没有权限.
                }
            }

            if (!permissionsList.isEmpty()) {
                String[] strings = new String[permissionsList.size()];
                ActivityCompat.requestPermissions(MerchantAddMap.this,permissionsList.toArray(strings), 0);
            }
        }
    }}